<template>
  <!--  品质列表-->
  <div class="purchaseOrderList">
    <jc-button
      :btn-arr="['reload', bomCost('check:save') && 'newy', 'remove', 'pass', 'sub', 'option']"
      :disab-arr="['close', 'cancel', 'com1', 'com2', 'using', 'copy', 'freeze']"
      :fbillno="fbillNo"
      url="tPurCheckout/queryPurCheckList"
      @refresh="handleGetUntreated"
      @newly="newAssem"
      @submit="submit(1)"
      @noSubmit="submit(2)"
      @audit="audit(1)"
      @noAudit="audit(2)"
      @toExcel="handleExport"
      @del="delectHandle"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQuerySonClass"
        />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="handleQuerySonClass"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        table-selection
        sortable
        get-row-keys="fentryId"
        :get-summaries="getSummaries"
        show-summary
        @selectionChange="selectionData"
      >
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="editParticulars(col.scope.row.fid)">{{ col.scope.row.fbillNo }}</el-link>
        </template>
        <!--    物料编码    -->
        <template v-slot:orderSlot="col">
          <el-link type="primary" @click="jumpMateriel(col.scope.row)">
            {{ col.scope.row.fnumber }}
          </el-link>
        </template>
        <template v-slot:billSlot="clo">
          <el-tag v-if="clo.scope.row.fcloseStatus==='未关闭'">{{ $t('market.normal') }}</el-tag>
          <el-tag v-else-if="clo.scope.row.fcloseStatus==='已关闭'">{{ $t('market.closed') }}</el-tag>
          <span v-else />
        </template>
        <template v-slot:btnState="col">
          <span v-if="col.scope.row.fdocumentStatus === ''" />
          <el-tag v-else :type="col.scope.row.stateColor">{{ col.scope.row.fdocumentStatus }}</el-tag>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
  </div>
</template>

<script>
import {
  purCheckUnAudit, purMrbAudit,
  purCheckAuSubmit, purCheckSubmit,
  purCheckDelete, queryPurCheckList
} from '@/api/quality/index'
import search from '@/components/Search'
import filtration from '@/components/filtration'
import searData from '@/components/Search/mixin'
import { toMxAmina } from '@/components/ToMxamineState'
import { maxDecimal } from '@/utils/number'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import jcButton from '@/components/Button'
export default {
  components: {
    search,
    jcButton,
    filtration
  },
  mixins: [searData],
  data() {
    return {
      ftype: 22,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      selectVal: [], // 多选
      fbillNo: '',
      // 表头
      tableBorder: false,
      tableData: [], // 销售数据
      tableHeader: [
        { label: this.$t('market.billType'), prop: 'fbilltype' },
        { label: this.$t('market.billNo'), type: 'tag', prop: 'fbillNo', width: '120' },
        { label: this.$t('market.fdate'), prop: 'fdate', width: '90' },
        { label: this.$t('market.checkouttype'), prop: 'fcheckouttype', width: '90' },
        { label: this.$t('market.state'), type: 'state', prop: 'fdocumentStatus', width: '90' },
        { label: this.$t('market.fmodel'), prop: 'fmodel' },
        { label: this.$t('market.fmaterial'), type: 'order', prop: 'fnumber', width: '110' },
        { label: this.$t('shop.fname2'), prop: 'fname', minWidth: '100' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecificaTion' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescripTion', minWidth: '200' },
        { label: this.$t('market.funit'), prop: 'funit', width: '80' },
        { label: this.$t('market.checkqty'), prop: 'fcheckqty', align: 'right', width: '80' },
        { label: this.$t('market.heqty'), prop: 'fheqty', align: 'right', width: '80' },
        { label: this.$t('market.note'), prop: 'fnote' },
        { label: this.$t('market.inSupplier'), prop: 'fsupplier', align: 'left', width: '100' },
        { label: this.$t('market.inWorkshop'), prop: 'fworkshop', width: '120' },
        { label: this.$t('market.result'), prop: 'fresult' },
        { label: this.$t('market.errReason'), prop: 'freason' }
      ],
      tableHeight: '70vh',
      tableHeader1: [
        { label: this.$t('market.paezText1'), prop: 'fpaeztext' }, { label: this.$t('market.note'), prop: 'fpaeztext1' },
        { label: this.$t('market.bankSupplier'), prop: 'fbacksupplier' }, { label: this.$t('market.bankWorkshop'), prop: 'fbackworkshop' },
        { label: this.$t('market.fprice'), prop: 'fprice' }, { label: this.$t('market.billamount'), prop: 'famount' },
        { label: this.$t('market.billamount_lc'), prop: 'famount_lc' }, { label: this.$t('market.ftaxPrice'), prop: 'ftaxprice' },
        { label: this.$t('market.billallamount'), prop: 'fallamount' }, { label: this.$t('market.billallamount_lc'), prop: 'fallamount_lc' },
        { label: this.$t('market.ftaxRate'), prop: 'ftaxrate' }, { label: this.$t('market.billtaxamount'), prop: 'ftaxamount' },
        { label: this.$t('market.billtaxamount_lc'), prop: 'ftaxrate' }, { label: this.$t('market.settleCurr'), prop: 'fsettlecurr' },
        { label: this.$t('market.localCurr'), prop: 'flocalcurr' }, { label: this.$t('market.creator'), prop: 'fcreator' },
        { label: this.$t('market.createDate'), prop: 'fcreateDate' }, { label: this.$t('market.lastmodifier'), prop: 'fmodifier' },
        { label: this.$t('market.lastmodifyDate'), prop: 'fmodifyDate' }, { label: this.$t('market.approver'), prop: 'fapprover' },
        { label: this.$t('market.approveDate'), prop: 'fapproveDate' }, { label: this.$t('market.billtaxamount'), prop: 'fbillamount' },
        { label: this.$t('market.billtaxamount_lc'), prop: 'fbilltaxamount_lc' }, { label: this.$t('market.billamount'), prop: 'fbillallamount' },
        { label: this.$t('market.billamount_lc'), prop: 'fbillallamount_lc' }, { label: this.$t('market.billallamount'), prop: 'fbilltaxamount' },
        { label: this.$t('market.billallamount_lc'), prop: 'fbilltaxamount_lc' }, { label: this.$t('shop.numIndex'), prop: 'fseq' }
      ],
      val: []
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader.concat(this.tableHeader1)
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '70vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.exportVal && this.exportVal.length > 0) {
          const list = this.exportVal.map(item => {
            return (toMxAmina(item))
          })
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '品质订单列表')
        } else {
          queryPurCheckList({
            pageNum: 1,
            pageSize: 100000,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array.map(item => {
                return (toMxAmina(item))
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '品质订单列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取品质列表数据
    async handleGetUntreated(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        search: this.searCollData.concat(this.details)
      }
      const { data: RES, data: total } = await queryPurCheckList(DATA)
      this.tableData = RES.array.map(item => {
        item.fprice = maxDecimal(item.fprice)
        return (toMxAmina(item))
      })
      this.total = total.total
      this.loading = false
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 价目详情
    editParticulars(id) {
      this.$router.push({ path: `/qualityDetail/${id}` })
    },
    // 多选 - 删除
    // 多选时触发的事件
    selectionData(val) {
      this.fbillNo = val[0].fbillNo
      this.exportVal = val
      this.selectVal = []
      if (val.length > 0) {
        val.forEach(res => {
          res.fbillNo && this.selectVal.push(res.fbillNo)
        })
      }
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: this.selectVal }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.selectVal = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
        this.handleGetUntreated()
      })
    },
    // 提交
    submit(ev) {
      const api = ev === 1 ? purCheckSubmit : purCheckAuSubmit
      this.toExamineFun(api)
    },
    // 审核
    audit(ev) {
      const api = ev === 1 ? purMrbAudit : purCheckUnAudit
      this.toExamineFun(api)
    },
    // 删除品质订单
    delectHandle() {
      this.$confirm('确定要删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const api = purCheckDelete
        this.toExamineFun(api)
      })
    },
    // 新增品质检验单
    newAssem() {
      this.$router.push({ path: '/createQuality' })
    },
    // 自定义列表
    customHandle(ev) {
      this.tableHeaders = []
      if (ev.header) {
        ev.header.forEach(item => {
          this.tableHeader1.forEach(res => {
            if (res.label === item) {
              this.tableHeaders.push(res)
            }
          })
        })
        this.tableHeader = this.tableHeaders
      } else if (ev.fname === '缺省方案') {
        this.tableHeader = this.oldTabHeader
      } else {
        this.tableHeader = this.tableHeader1
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.purchaseOrderList {
  @include listBom;
  .el-tag {
    height: 20px;
    line-height: 20px;
  }
  .header{
    position:relative;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
